/**
 * 二分查找
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var searchRange = function (nums, target) {
    let len = nums.length
    if (len === 0) return [-1, -1]

    // 二分查找找到mid
    let mid,
        left = 0,
        right = len - 1,
        isSearch = false
    while (left <= right) {
        mid = ~~((left + right) / 2)
        if (nums[mid] > target) {
            right = mid - 1
        } else if (nums[mid] < target) {
            left = mid + 1
        } else {
            isSearch = true
            break
        }
    }

    if (!isSearch) return [-1, -1]


    let equalLeft = mid,
        equalRight = mid
    
        // 寻找mid左侧等于target最小的索引值
        while (equalLeft >= 0 && nums[equalLeft] === target) {
        equalLeft--
    }
    
        // 寻找mid左侧等于target最小的索引值
    while (equalRight <= len - 1 && nums[equalRight] === target) {
        equalRight++
    }
    return [++equalLeft, --equalRight]
};

console.log(searchRange([2, 2], 3))